/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import ws.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.context.FacesContext;
import javax.faces.view.facelets.FaceletContext;
import javax.servlet.http.HttpSession;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.MessageContext;

public class SendMessageBean {

    private String title;
    private String message;
    private String receivername;

    public SendMessageBean() {
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getReceivername() {
        return receivername;
    }

    public void setReceivername(String receivername) {
        this.receivername = receivername;
    }

    public String sendMessage() {

        /*AutoCompleteBean backBean = (AutoCompleteBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("autoCompleteBean");
        receivername = backBean.getText();*/

        
        //FacesContext context = FacesContext.getCurrentInstance();
        //AutoCompleteBean autoCompleteBean = (AutoCompleteBean) context.getELContext().getELResolver().getValue(context.getELContext(), null, context);
        //AutoCompleteBean autoCompleteBean = (AutoCompleteBean) context.getApplication().evaluateExpressionGet(context, "#{autoCompleteBean}", AutoCompleteBean.class);
        //receivername = autoCompleteBean.getText();
         

        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        UserUI user = (UserUI) session.getAttribute("User");
        ws.RoMaWS_Service service = new ws.RoMaWS_Service();
        ws.RoMaWS port = service.getRoMaWSPort();
        //JAX-WX Auth.
        FacesContext context = FacesContext.getCurrentInstance();
        LoginBean loginBean = (LoginBean) context.getApplication().evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
        Map<String, Object> req_ctx = ((BindingProvider)port).getRequestContext();
        req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WSUrl.wsUrl);
        Map<String, List<String>> headers = new HashMap<String, List<String>>();
        headers.put("Username", Collections.singletonList(loginBean.getName()));
        headers.put("Password", Collections.singletonList(loginBean.getPassword()));
        req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
        if (port.sendPrivateMessage(receivername,title,message, user.getId())) {
            return "true";
        }
        return "false";
    }
    
    public List<String> complete(String query) {
        List<String> results = new ArrayList<String>();
        ws.RoMaWS_Service service = new ws.RoMaWS_Service();
        ws.RoMaWS port = service.getRoMaWSPort();
        //JAX-WX Auth.
        FacesContext context = FacesContext.getCurrentInstance();
        LoginBean loginBean = (LoginBean) context.getApplication().evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
        Map<String, Object> req_ctx = ((BindingProvider)port).getRequestContext();
        req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WSUrl.wsUrl);
        Map<String, List<String>> headers = new HashMap<String, List<String>>();
        headers.put("Username", Collections.singletonList(loginBean.getName()));
        headers.put("Password", Collections.singletonList(loginBean.getPassword()));
        req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
        results = port.getUserNamesLike(query);

        return results;
    }
}
